package vip.ola.pay.batchpay.sdk.utils;

import com.alibaba.fastjson.JSONObject;
import vip.ola.pay.batchpay.sdk.domain.Response;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class RemoteInvoker {
    private final static DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

	public static <T extends Response> T invoke(Object request, String url, Class<T> c) throws Exception {
		final String jsonData = request instanceof String? (String)request: JSONObject.toJSONString(request);
        final String sign = RsaUtils.sign(KeyUtil.getPrimaryKey(), jsonData);//签名

        Map<String , String> header = new HashMap<String , String>();
        header.put("x-efps-sign", sign);
        header.put("x-efps-sign-no", Config.getSignNo());
        header.put("x-efps-sign-type","SHA256withRSA");
        header.put("x-efps-timestamp",df.format(new Date()));
        header.put("Content-Type", "application/json");
        T response = HttpUtil.post(url, jsonData ,header , c);

        return response;
	}
}
